package com.xingdata.jjxc.activity;

import java.util.HashMap;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;

import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.base.Base1Activity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.enty.UpdateInfoEntity;
import com.xingdata.jjxc.enty.UserEntity;
import com.xingdata.jjxc.http.HttpResponseListener;
import com.xingdata.jjxc.http.HttpUtil;
import com.xingdata.jjxc.utils.Code;
import com.xingdata.jjxc.utils.ImageUtils;
import com.xingdata.jjxc.utils.SP;
import com.xingdata.jjxc.utils.StringUtil;
import com.xingdata.jjxc.utils.SystemInfo;
import com.xingdata.jjxc.views.CustomDialog;
import com.xingdata.jjxc.views.MyImageView;


/**没啥用了*/
public class WelcomActivity extends Base1Activity {
	private MyImageView imageView;
	private UpdateInfoEntity updateInfoEntity = null;

	@Override
	protected int getContentViewId() {
		// TODO Auto-generated method stub
		return R.layout.welcom;
	}

	@Override
	protected String getTitleText() {
		return null;
	}

	@Override
	protected void initViews() {
		imageView = (MyImageView) findViewById(R.id.welcom_bg);
		do_Post_softupdate();

	}

	private void do_Post_softupdate() {
		String url = "http://jjxc.hudim.com/index.php/Hud/public/softupdate";
		paramsMap = new HashMap<String, String>();
		paramsMap.put("version", SystemInfo.getAPKVersionName(this));
//		paramsMap.put("version", "0.01");
		paramsMap.put("package_name", "com.xingdata.jjxc");
		paramsMap.put("platform", "Android");
		HttpUtil.getInstance(this).Post(url, paramsMap,
				new HttpResponseListener() {

					@Override
					public void onSuccess(RespEntity respEntity) {
						updateInfoEntity = JSON.parseObject(
								respEntity.getResult(), UpdateInfoEntity.class);
						showUpadateDialog();
					}

					@Override
					public void onLoading(int vurrent) {

					}

					@Override
					public void onFailure(String result) {
						UserEntity user = SP.getUserInfo(WelcomActivity.this);
						if (user != null) {
							if (user.getMobile().length() != 0
									&& user.getPw().length() != 0) {
								showProgressDialog("正在登录...");
								doPost_login(user.getMobile(), user.getPw());
							} else {
								startActivity(new Intent(WelcomActivity.this,
										LoginActivity.class));
								finish();
							}

						}
					}
				});
	}

	public void showUpadateDialog() {
		CustomDialog.Builder builder = new CustomDialog.Builder(this,
				R.layout.dialog_update_info);
		StringBuffer buffer = new StringBuffer();
		buffer.append("发现新版本" + updateInfoEntity.getVersion_no() + "\n\n");
		buffer.append("升级内容 " + "\n");
		buffer.append(updateInfoEntity.getVersion_desc().replaceAll(" ", "\n")
				+ "\n");
		buffer.append("总大小："
				+ StringUtil.formatSize(updateInfoEntity.getFile_size()));
		builder.setMessage(buffer.toString());
		builder.setTitle("提示");
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				startActivity(new Intent(Intent.ACTION_VIEW, Uri
						.parse(updateInfoEntity.getVersion_path())));
				dialog.dismiss();
			}
		});

		builder.setNegativeButton("取消",
				new android.content.DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						finish();
					}
				});

		builder.create().show();
	}

	private void doPost_login(final String mobile, final String password) {
		if (isNetworkConnected() == false) {
			showToast("网络连接断开，请检查网络");
			return;
		}
		paramsMap = new HashMap<String, String>();
		paramsMap.put("mobile", mobile);
		paramsMap.put("password", password);
		mHttpUtil.Post(App.ZZD_REQUEST_LOGIN, paramsMap,
				new HttpResponseListener() {

					@Override
					public void onSuccess(RespEntity entity) {
						dismissProgressDialog();
						// SharedPreTools.putString(LoginActivity.this,
						// Common.USERNAME, mobile);
						// SharedPreTools.putString(LoginActivity.this,
						// Common.PASSWORD,Jni.Jni_encryptionStr(password,
						// mobile));
						UserEntity user = JSON.parseObject(entity.getResult(),
								UserEntity.class);
						// 获取Token
						TokenEntity tokenEntity = new TokenEntity();
						tokenEntity.setToken(Code.accessToken(mobile, password,
								user.getUserid()));
						SystemInfo.setTokenEntity(tokenEntity);
						user.setMobile(mobile);
						user.setToken(tokenEntity.getToken());
						user.setPw(password);
						SP.saveUserInfo(WelcomActivity.this, user);
						// baocuo
						// carHudlist =
						// JUtils.getcarHudList(entity.getHudlist());
						// SP.saveHudInfo(LoginActivity.this,
						// entity.getHudlist());
						nextController();
					}

					@Override
					public void onFailure(String result) {
						showToast(result);
						dismissProgressDialog();
						startActivity(new Intent(WelcomActivity.this,LoginActivity.class));
						finish();
					}

					@Override
					public void onLoading(int vurrent) {
						// TODO Auto-generated method stub

					}
				});
	}

	private void nextController() {
		startActivity(new Intent(WelcomActivity.this, MainActivity.class));
		finish();
	};

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		ImageUtils.releaseImageViewResouce(imageView);
	}
}
